package com.guqinghan.guqinghan_02_systemfile;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ShareActionProvider;
import android.widget.Toast;

/*
    轻量级存储 SharePreferences
 */
public class SharePreferencesActivity extends AppCompatActivity {

    private EditText et_read;
    private EditText et_write;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_preferences);
        et_read = findViewById(R.id.et_read);
        et_write = findViewById(R.id.et_write);
    }


    public void writeInfo(View view) {
        // 获取SharedPreferences对象
        SharedPreferences sp = SharePreferencesActivity.this.getSharedPreferences("write.txt", Context.MODE_PRIVATE);
        // 获取Editor对象，进行存储
        SharedPreferences.Editor edit = sp.edit();
        String writeSp = et_write.getText().toString();
        edit.putString("write",writeSp);
        edit.commit();
    }

    public void readInfo(View view) {
        // 获取SharedPreferences对象
        SharedPreferences sp = SharePreferencesActivity.this.getSharedPreferences("write.txt", Context.MODE_PRIVATE);
        String string = sp.getString("write", "没有取到");
        et_read.setText(string);
        Toast.makeText(SharePreferencesActivity.this,string,Toast.LENGTH_LONG).show();
    }

}
